package tables

import (
	"peilian-api/app/global/variable"

	"gorm.io/gorm"
)

const (
	ConfKeyAverageScore    = "average_score"
	ConfKeyAbilityComments = "ability_comments"
	ConfKeyUserNoReport    = "user_no_report"
	ConfKeyProfontVideo    = "profont_video"
	ConfKeyRoleUri         = "role_uri"
	ConfKeyShareWriting    = "share_writing"
	ConfKeyCarousel        = "carousel"
	ConfKeyBdCarousel      = "bd_carousel"
	ConfKeySpecialTab      = "special_tag"
	ConfKeySchool          = "school"
	ConfKeyMember          = "member"
	ConfKeyTopicNew        = "topic_new"
	ConfKeyGreeting        = "greeting"

	AverageScoreType = 1
	AbilityComments  = 2
	UserNoReport     = 3
	ProfontVideo     = 4
)

type CommonConf struct {
	gorm.Model
	ConfKey    string `json:"conf_key" gorm:"type:varchar(20);comment:指定key"`
	Type       uint   `json:"type" gorm:"type:tinyint(3);default:0;comment:类型"`
	ConfValue  string `json:"conf_value" gorm:"type:longtext;comment:详细信息"`
	Remarks    string `json:"remarks" gorm:"type:varchar(256);default:\"\";comment:详细备注"`
	ExtendInfo string `json:"extend_info" gorm:"type:varchar(1024);default:\"\";comment:扩展信息"`
	Other      string `json:"other" gorm:"type:varchar(256);default:\"\";comment:其他信息"`
}

func (q *CommonConf) TableName() string {
	return "common_conf"
}

func (q *CommonConf) FetchByType(typ int) ([]CommonConf, error) {
	var conf []CommonConf
	if d := variable.DB.MySQL.Model(&CommonConf{}).Where("type = ?", typ).Find(&conf); d.Error != nil {
		return conf, d.Error
	}
	return conf, nil
}

func (q *CommonConf) FetchByConfKey(ConfKey string) ([]CommonConf, error) {
	var conf []CommonConf
	if d := variable.DB.MySQL.Model(&CommonConf{}).Where("conf_key = ?", ConfKey).Find(&conf); d.Error != nil {
		return conf, d.Error
	}
	return conf, nil
}

func (q *CommonConf) FetchByTypeKey(ConfKey string, typ int) ([]CommonConf, error) {
	var conf []CommonConf
	if d := variable.DB.MySQL.Model(&CommonConf{}).Where("conf_key = ? and type = ?", ConfKey, typ).Find(&conf); d.Error != nil {
		return conf, d.Error
	}
	return conf, nil
}

func (q *CommonConf) FetchByWhereMap(where map[string]interface{}) ([]CommonConf, error) {
	var conf []CommonConf
	if d := variable.DB.MySQL.Model(&CommonConf{}).Where(where).Find(&conf); d.Error != nil {
		return conf, d.Error
	}
	return conf, nil
}

func (q *CommonConf) FetchByWhereStr(whereStr string) ([]CommonConf, error) {
	var conf []CommonConf
	if d := variable.DB.MySQL.Model(&CommonConf{}).Where(whereStr).Find(&conf); d.Error != nil {
		return conf, d.Error
	}
	return conf, nil
}

func (q *CommonConf) UpsertByWhere(whereMap map[string]interface{}, updateMap map[string]interface{}) error {
	// 1. 先根据条件查询是否存在
	var cof CommonConf
	d := variable.DB.MySQL.Model(&CommonConf{}).Where(whereMap).First(&cof)
	if d.Error != nil {
		if d.Error == gorm.ErrRecordNotFound {
			// 插入
			if _, ok := updateMap["conf_key"]; ok {
				cof.ConfKey = updateMap["conf_key"].(string)
			}
			if _, ok := updateMap["conf_value"]; ok {
				cof.ConfValue = updateMap["conf_value"].(string)
			}
			if _, ok := updateMap["extend_info"]; ok {
				cof.ExtendInfo = updateMap["extend_info"].(string)
			}
			if _, ok := updateMap["remarks"]; ok {
				cof.Remarks = updateMap["remarks"].(string)
			}
			if _, ok := updateMap["other"]; ok {
				cof.Other = updateMap["other"].(string)
			}
			if _, ok := updateMap["type"]; ok {
				cof.Type = uint(updateMap["type"].(int))
			}
			d2 := variable.DB.MySQL.Model(&CommonConf{}).Create(&cof)
			if d2.Error != nil {
				return d2.Error
			}
			return nil
		}
	} else {
		d2 := variable.DB.MySQL.Model(&CommonConf{}).Where("id = ?", cof.ID).Updates(updateMap)
		if d2.Error != nil {
			return d2.Error
		}
	}
	return nil
}
